package com.wing.oauth2.details.userdetails.warehouse;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.wing.common.constant.GlobalConstants;
import com.wing.warehouse.model.response.SelContactInfoAuthVO;
import com.wing.warehouse.model.response.WarehouseUserAuthVO;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;


/**
 * 系统管理用户认证信息
 *
 * @author
 */
@Data
public class WarehouseUserDetails implements UserDetails {

    /**
     * 扩展字段
     */
    private String authMethod;

    /**
     * 默认字段
     */
    private String username;
    private String password;
    private Boolean enabled;
    private Collection<SimpleGrantedAuthority> authorities;


    private Long id;

    private String account;


    private String name;

    private String position;

    private String phone;

    private String photo;

    private String visitCard;

    private Integer sex;

    private String department;

    private String address;

    private String wechat;

    private String mail;

    private Integer sort;

    private Long warehouseCustomerId;
    private Long warehouseUserId;

    private Integer roleId;

    /**
     * 系统管理用户
     */
    public WarehouseUserDetails(SelContactInfoAuthVO user) {
        this.setId(user.getId());
        this.setUsername(user.getAccount());
        this.setPassword(user.getPassword());
        this.setName(user.getName());
        this.setPhone(user.getTelephone());
        this.setRoleId(user.getRoleId());
        this.setWarehouseCustomerId(user.getSelCustomerId());
        this.setWarehouseUserId(user.getId());

//        if (CollectionUtil.isNotEmpty(user.getRoles())) {
            authorities = new ArrayList<>();
            authorities.add(new SimpleGrantedAuthority(this.getRoleId().toString()));
//        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return this.enabled;
    }

}
